package tech.yunland.shop.domain.handler;

import com.moyuer.bsf.domain.model.Organ;
import com.moyuer.bsf.domain.model.User;
import com.moyuer.bsf.domain.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tech.yunland.shop.domain.model.GoodsPaid;
import tech.yunland.shop.domain.service.GoodsPaidService;
import tech.yunland.shop.domain.service.WeiXinServices;
import tech.yunland.weixin.domain.model.OrganWexin;
import tech.yunland.weixin.domain.service.OrganWexinService;
import tech.yunland.weixin.handler.MessageHandler;
import tech.yunland.weixin.protocol.BaseArticle;
import tech.yunland.weixin.protocol.BaseMessage;
import tech.yunland.weixin.protocol.message.TextMessage;
import tech.yunland.weixin.protocol.send.reply.NewsReplyMsg;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * 会员余额查询
 * 实现微信模块中的MessageHandler 便可处理相应的消息
 * 这里处理的是余额查询
 * @author 李珂<likenamehaojie@hotmail.com>
 */
@Component
public class GoodsPaidMessageHandler implements MessageHandler<BaseMessage> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${weixin.api.host_url}")
    private String host;

    @Autowired
    private UserService userService;

    @Autowired
    private WeiXinServices weiXinServices;

    @Autowired
    private OrganWexinService organWexinService;

    @Autowired
    private GoodsPaidService goodsPaidService;

    @Override
    public BaseMessage deal(BaseMessage inputMessage) {
        logger.info(this.getClass().getName(),"正在处理查询优惠活动事件");
  /*      TextMessage result = new TextMessage();
        result.setFromUser(inputMessage.getToUser());
        result.setToUser(inputMessage.getFromUser());
        result.setContent(this.getPaid(inputMessage));
        result.setCreateTime(new Date());
        result.setType(BaseMessage.MsgType.text);

        return result;*/
        if(this.getPaid(inputMessage).equals("请先绑定")){
            NewsReplyMsg result = new NewsReplyMsg();
            result.setFromUser(inputMessage.getToUser());
            result.setToUser(inputMessage.getFromUser());
            result.setCreateTime(new Date());
            result.setType(BaseMessage.MsgType.news);
            List<BaseArticle> articleList = new ArrayList<BaseArticle>();
            BaseArticle article = new BaseArticle();
            article.setTitle("请先绑定会员卡");
            article.setDescription("未绑定会员卡不可进行查询操作");
            article.setPicUrl("http://api.yunlandtech.com/uploads/52dd658e-7e54-4ac8-ada5-ee6524550a7f.jpg");
            OrganWexin organWexin = organWexinService.getBywxNumber(inputMessage.getToUser());
            Organ organ = organWexin.getOwner();
            String organId = organ.getId();

            article.setUrl(host + "/#/bind/"+inputMessage.getToUser()+"/"+inputMessage.getFromUser()+"?organId="+organId);
            articleList.add(article);
            result.setArticles(articleList);
            result.setArticleCount(articleList.size());
            return result;

        }else {
            TextMessage result = new TextMessage();
            result.setFromUser(inputMessage.getToUser());
            result.setToUser(inputMessage.getFromUser());
            result.setContent(this.getPaid(inputMessage));
            result.setCreateTime(new Date());
            result.setType(BaseMessage.MsgType.text);

            return result;
        }
    }

    public String getPaid(BaseMessage inputMessage) {

        StringBuffer buffer = new StringBuffer();

        User user = userService.get(inputMessage.getToUser(), inputMessage.getFromUser());
        if (user == null) {
            return "请先绑定";
        }

        OrganWexin organWexin = organWexinService.getBywxNumber(inputMessage.getToUser());
        Organ organ = organWexin.getOwner();
 /*       String organId = weiXinServices.getByOpenId(inputMessage.getFromUser());*/
        List<GoodsPaid> result = goodsPaidService.getList(user.getId(), organ.getId());

        if (result.size() == 0) {
            buffer.append("您的账户空空如也, 及时充值可享受更多优惠!");
            return buffer.toString();
        }

        GoodsPaid goodsPaidone = result.get(0);
        buffer.append("尊敬的" + goodsPaidone.getMemberName() + ", ");
        buffer.append("以下是您的账户清单: ").append(System.lineSeparator());
        buffer.append("--------------------").append(System.lineSeparator());

        for (GoodsPaid goodsPaid : result) {
            buffer.append(goodsPaid.getGoods().getName())
                    .append(" ").append(goodsPaid.getQuantity()).append(goodsPaid.getGoods().getUnit())
                    .append(System.lineSeparator())
                    .append("价值 ").append(goodsPaid.getGoods().getPrice().multiply(goodsPaid.getQuantity())).append("元")
                    .append(System.lineSeparator());
            buffer.append("--------------------").append(System.lineSeparator());
        }
        buffer.append("如有疑问, 请到店咨询。");

        return buffer.toString();
    }

}

